// created by noellynch
// May 12, 2009

package com.sceneworker.editors.skybox;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.Callable;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;

import com.jme.image.Texture;
import com.jme.scene.Skybox;
import com.jme.util.GameTaskQueueManager;
import com.jme.util.TextureManager;
import com.myjavautils.GenericUtils;
import com.myjavautils.swing.MigLayoutUtils;
import com.myjavautils.swing.SimpleAlert;
import com.myjavautils.swing.SimpleImage;
import com.myjavautils.swing.SwingComponentFactory;
import com.sceneworker.app.globals.SceneWorkerGlobals;

public class SkyBoxEditorPanel extends JPanel {

	private		JDialog		mcl_par;
	private		Skybox		mcl_skyBox;
	
	private		SimpleImage		mcl_north;
	private		SimpleImage		mcl_south;
	private		SimpleImage		mcl_east;
	private		SimpleImage		mcl_west;
	private		SimpleImage		mcl_up;
	private		SimpleImage		mcl_down;
	
	public	SkyBoxEditorPanel(Skybox cl_skyBox, JDialog cl_par) {
		mcl_skyBox = cl_skyBox;
		mcl_par = cl_par;
		
		MigLayoutUtils.asMigLayout(this);
		
		mcl_north = makeSimpleImage("North", 5, 5, false);
		mcl_south = makeSimpleImage("South", 5, 210, true);
		mcl_east = makeSimpleImage("East", 5, 415, false);
		
		mcl_west = makeSimpleImage("West", 305, 5, true);
		mcl_up = makeSimpleImage("Up", 305, 210, false);
		mcl_down = makeSimpleImage("Down", 305, 415, true);
		
		JButton		lcl_create = SwingComponentFactory.makeAButton("Create");
		lcl_create.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				createSkyBox();
				mcl_par.setVisible(false);
			}
		});
		
		
		JButton		lcl_cancel = SwingComponentFactory.makeAButton("Cancel");
		lcl_cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				mcl_par.setVisible(false);
			}
		});
		MigLayoutUtils.makeRightAlignedButtonBar(this, lcl_cancel, lcl_create);
	}
	
	private		SimpleImage		makeSimpleImage(String str_label, int i_x, int i_y, boolean b_wrap) {
		final	SimpleImage		lcl_im = new SimpleImage();
		
		JButton			lcl_btn = SwingComponentFactory.makeAButton(str_label);
		
		lcl_btn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//File		lcl_f = SceneWorkerPreferences.inst().getAFile(lcl_im, false, false);
				File		lcl_f = GenericUtils.getAnImageFile(lcl_im);
				if(lcl_f == null) {
					return;
				}
				
				try {
					BufferedImage		lcl_image = ImageIO.read(lcl_f);
					lcl_im.setImage(lcl_image);
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		
		add(lcl_im);
		
		if(b_wrap) {
			add(lcl_btn, "wrap");
		} else {
			add(lcl_btn);
		}
		
		return lcl_im;
	}
	
	private		void	createSkyBox() {
		if((mcl_north.getImage() == null) || (mcl_south.getImage() == null) || (mcl_east.getImage() == null) || (mcl_west.getImage() == null) || (mcl_up.getImage() == null) || (mcl_down.getImage() == null)) {
			SimpleAlert.doAlert("You have not selected all the images to create the sky box.", this);
			return;
		}
		
		GameTaskQueueManager.getManager().update(new Callable<Void>() {

			public Void call() throws Exception {
				makeAFace(mcl_skyBox, mcl_north.getImage(), Skybox.Face.North);
				makeAFace(mcl_skyBox, mcl_south.getImage(), Skybox.Face.South);
				makeAFace(mcl_skyBox, mcl_east.getImage(), Skybox.Face.East);
				makeAFace(mcl_skyBox, mcl_west.getImage(), Skybox.Face.West);
				makeAFace(mcl_skyBox, mcl_up.getImage(), Skybox.Face.Up);
				makeAFace(mcl_skyBox, mcl_down.getImage(), Skybox.Face.Down);
				
				mcl_skyBox.updateRenderState();
				return null;
			}
			
		});
	}
	
	private		void		makeAFace(Skybox cl_box, BufferedImage cl_im, Skybox.Face cl_face) {
		Texture		lcl_texture = TextureManager.loadTexture(cl_im, Texture.MinificationFilter.Trilinear, Texture.MagnificationFilter.Bilinear, true);
		cl_box.setTexture(cl_face, lcl_texture);
	}
}
